--
-- Author: ping.wang
-- Date: 2018-09-03 11:14:28
--
-- PeeragesPanel
-- 爵位界面
--

local ClassRef = app.mvc.createPanelClass(...)
local PeeragesKingNodeRender = import(".renders.PeeragesKingNodeRender")
local PeeragesListNodeRender = import(".renders.PeeragesListNodeRender")
local NewguildHomeLeftRender = import(".renders.NewguildHomeLeftRender")
local table_insert = table.insert
local ListViewExtend = require("app.widget.ListViewExtend")
--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewGuildEmperorNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )
    self._guildHomeLeftRender = NewguildHomeLeftRender.create(self._ui.left)--界面左边部分 公会信息


    local btn = self._ui.Button
    btn:addClickEventListener(function()
        app:sendMsg("WorldBattleController", "openView" )
    end)
    btn:findChild("name"):setString(L("emperor_road3"))

    self._ui["Panel/Panel_right/Panel_bom/Text"]:setString(L("emperor_road2"))

    local tipsImg = self._ui["Panel/Panel_right/Panel_bom/btn_tips"]
    tipsImg:setTouchEnabled(true)
    tipsImg:addClickEventListener(function()
        executeFunc(self._onOperate, "showEmperorBookView")
    end)


    --self._listView = self._ui.ListView --下拉列表 来显示所有爵位信息
    self._listView = ListViewExtend.scrollToItemExtend(self._ui.ListView)
    self._haveInit = false
end

function ClassRef:refresh(data)
    self._data = data
    if self._data then
        --刷新左侧
        self._guildHomeLeftRender:refreshGuildInfo(data)
        --刷新帝王之路界面
        self:setVisible( true )
        self._ui.tlt:setString(string.format(L("emperor_road1"),data:getGuildLeaderName()))

        local maxActivePeerageId = self._data:getMaxActiveTerritoryId()
        local maxActivePeerage = nil
        local haveTown = false --代表是否有领地
        local ownTownList = data:getOwnTownList()
        if ownTownList and next(ownTownList) then
            haveTown = true
        end
        if not self._haveInit then
            self._allRenders = {}
            local forceInfo = GD:queryGuildForceInfos()
            for k,v in ipairs(forceInfo) do
                local peerageId = tonumber(v.peerageID)
                local isMin = false --是否是最小爵位

                if k == 1 then
                    local kingRender = PeeragesKingNodeRender.createWithData({peerageId,data,haveTown})
                    kingRender:onBtnClick(self._onOperate)
                    self._listView:pushBackCustomItem(kingRender)
                    table_insert(self._allRenders,kingRender)
                else
                    if k == #forceInfo then
                        isMin = true
                    end

                    local tmpRender = PeeragesListNodeRender.createWithData({peerageId,data,isMin,(k-1)%3,haveTown})
                    tmpRender:onBtnClick(self._onOperate)
                    self._listView:pushBackCustomItem(tmpRender)
                    table_insert(self._allRenders,tmpRender)
                end

                if (maxActivePeerageId == 0 and isMin ) or (maxActivePeerageId == peerageId and k == 1) or (maxActivePeerageId + 1) == peerageId then
                    maxActivePeerage = self._allRenders[k]
                end
            end

            self._listView:performWithDelay(function()
                if cc.isAlive(maxActivePeerage) then
                    self._listView:scrollToItem(maxActivePeerage, 0.8)
                end
            end, 0)
            self._haveInit = true
        else
            --若已初始化完成 则只用刷新
            local forceInfo = GD:queryGuildForceInfos()

            for k,v in ipairs(forceInfo) do
                local peerageId = tonumber(v.peerageID)
                local isMin = false --是否是最小爵位

                if k == 1 then
                    local kingRender = self._allRenders[k]
                    kingRender:setData({peerageId,data,haveTown})
                else
                    if k == #forceInfo then
                        isMin = true
                    end
                    local tmpRender = self._allRenders[k]
                    tmpRender:setData({peerageId,data,isMin,(k-1)%3,haveTown})
                end


                if (maxActivePeerageId == 0 and isMin ) or (maxActivePeerageId == peerageId and k == 1) or (maxActivePeerageId + 1) == peerageId then
                    maxActivePeerage = self._allRenders[k]
                end
            end
            self._listView:performWithDelay(function()
                if cc.isAlive(maxActivePeerage) then
                    self._listView:scrollToItem(maxActivePeerage, 0.8)
                end
            end, 0)
        end
    else
        self:setVisible( false )
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
    self._guildHomeLeftRender:onOperate(self._onOperate)
end

return ClassRef
